package com.zsoft.webmail.servlet.hainannet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.zsoft.webmail.*;
import com.zsoft.webmail.mimeutility.*;
import com.zsoft.webmail.Application;
import comm.util.Utils;
import com.zsoft.webmail.RandomAccessBufferFile;
import com.zsoft.webmail.folder.*;

public class MailSourceServlet extends HttpServlet {
  //private static final String CONTENT_TYPE = Application.getProperty("ContentType");
  private static final String CONTENT_TYPE = "text/html";
  /**Initialize global variables*/
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    comm.http.Request request1=new comm.http.Request(request,Application.getProperty("defaultInputCharset"));

    //PrintWriter out = response.getWriter();
    String mimeCharset1=Application.getProperty("defaultMimeCharset");
    if(mimeCharset1==null || mimeCharset1.equals(""))
    mimeCharset1 = com.zsoft.webmail.mimeutility.MimeUtility.mimeCharset(com.zsoft.webmail.mimeutility.MimeUtility.getDefaultJavaCharset());
    String javaCharset1=com.zsoft.webmail.mimeutility.MimeUtility.javaCharset(mimeCharset1);
    comm.http.ResponseOut out= new comm.http.ResponseOut(response,javaCharset1,false);


    MemberAuthorization memberAuthToken=null ;
    HttpSession session=request.getSession();
    Object object = session.getAttribute("authBean");
    if(object==null){
      session.setAttribute("authBean",new com.zsoft.webmail.AuthBean(session.getId(),request.getRemoteAddr()));
      response.sendRedirect( "login.jsp" );
      return;
    }else{
      memberAuthToken = ((AuthBean)object).getMemberAuthToken();
      if(memberAuthToken==null){
        response.sendRedirect( "login.jsp" );
        return;
      }
    }







String boxid=request1.getParameter("boxid");
String mid=request1.getParameter("mid");
boxid=comm.util.FileUtils.getFilename(boxid);
mid=comm.util.FileUtils.getFilename(mid);
// get all relevant paths
String webPath=memberAuthToken.webPath;
String userPath=memberAuthToken.homePath;
FolderList fl=null;
try{
  fl=new FolderList(userPath+"/directory.cfg");
}catch(Exception e){}
char sep='/';
	String msgPath=null;
	if ( boxid != null )
	{
          msgPath = userPath+fl.getValueById(boxid);
        }else{
          msgPath = webPath;
        }
try{
String charset="gb2312";
try{
  com.zsoft.webmail.message.Header header= new com.zsoft.webmail.message.Header(msgPath+sep+mid+".hdr");
  charset=header.getValue("Charset");

}catch(Exception ex){

}
if(charset==null)charset="";
if(charset.trim().equals("")) charset=MimeUtility.mimeCharset( MimeUtility.getDefaultJavaCharset());

response.setContentType("text/html");
//RandomAccessBufferFile raf=new RandomAccessBufferFile(msgPath+sep+mid);
RandomAccessBufferFile raf=null;
out.println("<html>");
out.println("<head>");
out.println("<meta content=\"text/html; charset="+charset+"\" http-equiv=\"Content-Type\">");
out.println("</head>");
out.println("<body>");
out.println("<pre>");
try{
raf=new RandomAccessBufferFile(msgPath+sep+mid);
raf.setCharset(MimeUtility.javaCharset(charset));


String LB="&lt";
String RB="&gt";

while ( true )
{
        String buf = raf.readBufLine();
        if(buf ==null) break;
        StringBuffer sbuf=new StringBuffer(buf);
        int beginIdx=0;

        while ( (beginIdx=sbuf.toString().indexOf("<")) != -1 )
                sbuf = sbuf.replace(beginIdx, beginIdx+1, LB);

        while ( (beginIdx=sbuf.toString().indexOf(">")) != -1 )
                sbuf = sbuf.replace(beginIdx, beginIdx+1, RB);

        out.println(sbuf.toString());
        sbuf=null;
        buf=null;
}
}finally{
if(raf!=null)raf.close();
}
}catch(Exception e){

}

out.println("</pre>");

out.println("</body>");
out.println("</html>");
}

  /**Process the HTTP Post request*/
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
  }
  /**Clean up resources*/
  public void destroy() {
  }
}